package cn.gudian.taglib.cms.customer

import cn.gudian.cms.News.ArcType

class CusArcTypeTagLib {
       static namespace = "gd"    //定义该系统的命名空间
  /**
   * 显示所有栏目列表或者该级栏目同级及所有下级列表
   * <gd:arcTypeShow arcTypeID="${params?.id}" nowID="${params?.id}"></gd:arcTypeShow>
   * arcTypeID 如果有该参数，则显示该级栏目同级及所有下级列表，没有的话表示显示所有栏目列表
   * nowID  表示当前栏目,用样式now表示
   */
  def arcTypeShow = {attrs, body ->
    def nowID = attrs?.nowID.toString()
    def arcTypeID = attrs?.arcTypeID
    def mode=attrs?.mode
    def path=attrs?.path
    def arcTypeInstanceList = null
    if (arcTypeID && ArcType.get(Long.parseLong(arcTypeID)).getParent()?.id != null) {
      def id = ArcType.get(Long.parseLong(arcTypeID)).getParent()?.id
      arcTypeInstanceList = ArcType.findAll("from ArcType where mode=? and  parent_id=?", [mode,id]);
    } else {
      arcTypeInstanceList = ArcType.findAll("from ArcType where mode=? and  parent=null",[mode]);
    }

    if (arcTypeInstanceList) {
      out << "<ul>"
      out << body()
      arcTypeInstanceList.each {
        out << "<li><a ${it?.id.toString() == nowID ? 'class="now"' : ''} href='"+path+"/"+it.id+"'>" + it.name + "</a></li>"
        arcType(it?.id, nowID,mode,path)
      }
      out << "</ul>"
    }

  }

  /**
   * 递归显示该级栏目下面的所有子级列表
   */
  def arcType = {arcTypeID, nowID,mode,path ->
    def arcTypeInstanceList = ArcType.findAll("from ArcType  where mode=? and  parent_id=?", [mode,arcTypeID])
    if (arcTypeInstanceList) {
      out << "<ul>"
      arcTypeInstanceList?.each {
        out << "<li><a   ${it?.id.toString() == nowID ? 'class="now"' : ''} href='"+path+"/"+it.id+"'>" + it.name + "</a></li>"
        arcType(it?.id, nowID,mode,path)
      }
      out << "</ul>"
    }
  }
}
